// 引入mongoose第三方模块
const mongoose = require('mongoose');

const bookSchema = new mongoose.Schema({
    //作者id
    aid:{
        type:mongoose.Schema.Types.ObjectId
    },
  
    //作者
    book_author:{
        type: String,
        maxlength: 10,   //字符串长度
        minlength: 2,
        required: [true, "作者"],
        trim:true,    //自动去两侧的空格，lowercase,uppercase
    },
    //书名字
    book_name: {
        type: String,
        maxlength: 20,   //字符串长度 
        minlength: 2,
        required: [true, "书名字"],
        unique:true,   //表示这个属性不允许重复
        trim:true,    //自动去两侧的空格，lowercase,uppercase
    },
 
    //默认创建时间
    registerDate: { 
        type: Date,
        default: Date.now //默认值
    }
})

// 创建集合,首字母必须是 大写的 User
const Book = mongoose.model('Book', bookSchema);

//新增一个
async function add1() { 
    var data1 = await new Book({
        book_author: "admin", 
        book_name: "第3本书名",
    }).save()
    console.log(data1)
}
 

// add1()


module.exports=Book